package com.lwb.ztx.user.widget;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.lwb.ztx.R;
import com.lwb.ztx.user.widget.content.UserItem;
import com.lwb.ztx.utils.ListUtils;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by luweibin on 2016/5/31.
 */
public class UserItemAdapter extends RecyclerView.Adapter {
    private List<UserItem> items;

    private OnItemClickListener listener;

    public UserItemAdapter(List<UserItem> items, OnItemClickListener listener) {
        this.items = items;
        this.listener = listener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.user_item, parent, false);
        return new UserViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        UserViewHolder userViewHolder = (UserViewHolder) holder;
        userViewHolder.itemIcon.setImageResource(items.get(position).getItemResId());
        userViewHolder.itemName.setText(items.get(position).getItemName());
        userViewHolder.item = items.get(position);
    }

    @Override
    public int getItemCount() {
        return ListUtils.getSize(items);
    }

    class UserViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        @InjectView(R.id.user_item_icon)
        ImageView itemIcon;
        @InjectView(R.id.user_item_name)
        TextView itemName;
        UserItem item;
        public UserViewHolder(View itemView) {
            super(itemView);
            ButterKnife.inject(this,itemView);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if(listener != null && item!= null) {
                listener.onClick(item);
            }
        }
    }

    public interface OnItemClickListener {
        void onClick(UserItem item);
    }

}
